package com.diver.flinkdemo.service.impl;

import com.diver.flinkdemo.dto.xxt.DeviceModeVO;
import com.diver.flinkdemo.entity.xxt.DeviceMode;
import com.diver.flinkdemo.mapper.mysqlMapper.DeviceModeMapper;
import com.diver.flinkdemo.service.IDeviceModeService;
import com.diver.flinkdemo.service.IRedisService;
import com.diver.flinkdemo.utils.DynamicDataSourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ywy
 * 2021/6/16 11:36
 */
//@Service
@Slf4j
public class DeviceModeServiceImpl implements IDeviceModeService {

//    @Autowired
//    private SchoolModeMapper schoolModeMapper;
//    @Autowired
    private DeviceModeMapper mapper;
    private SqlSession session;
//    @Autowired
    private IRedisService redisService = new RedisServiceImpl();



//    @Override
//    public void saveDeviceMode(DeviceMode deviceMode) {
//        String modeId = deviceMode.getId();
//        // 添加
//        if (StringUtils.isEmpty(modeId)) {
//            modeId = UUIDUtil.uuid();
//            deviceMode.setId(modeId);
//            deviceMode.setCreateTime(System.currentTimeMillis());
//            if (null == deviceMode.getGpsColFlag()){
//                deviceMode.setGpsColFlag(0);
//            }
//            if (null == deviceMode.getGpsColInterval()){
//                deviceMode.setGpsColInterval(0);
//            }
//            if (null == deviceMode.getGpsRepFlag()){
//                deviceMode.setGpsRepFlag(0);
//            }
//            this.save(deviceMode);
//        } else {    // 更新
//            deviceMode.setUpdateTime(System.currentTimeMillis());
//            this.updateById(deviceMode);
//        }
//        //更新缓存
//        /*List<School> useSchool = mapper.selectUseSchool(modeId);
//        if (!CollectionUtils.isEmpty(useSchool)) {
//            for (School school : useSchool) {
//                String key = CommonResource.SCHOOL_MODE_LIST + school.getId();
//                redisService.remove(key);
//                List<DeviceModeVO> modes = getSchoolMode(school.getId());
//                if (!CollectionUtils.isEmpty(modes)){
//                    redisService.set(key, modes);
//                }
//            }
//        }*/
//    }

//    @Override
//    public IPage<DeviceModeVO> deviceModePageList(Integer pageNum, Integer pageSize) {
//        IPage<DeviceMode> page = new Page<>(pageNum, pageSize);
//        page = this.page(page, new QueryWrapper<>());
//        return page2PageVo(page);
//    }
//
//    private IPage<DeviceModeVO> page2PageVo(IPage<DeviceMode> page) {
//        IPage<DeviceModeVO> pageVo = new Page<>();
//        pageVo.setCurrent(page.getCurrent());
//        pageVo.setPages(page.getPages());
//        pageVo.setSize(page.getSize());
//        pageVo.setTotal(page.getTotal());
//
//        List<DeviceModeVO> modeVOList = new ArrayList<>();
//        List<DeviceMode> modeList = page.getRecords();
//        modeList.forEach(deviceMode -> {
//            DeviceModeVO deviceModeVO = new DeviceModeVO();
//            BeanUtils.copyProperties(deviceMode, deviceModeVO);
//            List<SchoolMode> schoolModeList = schoolModeMapper.selectList(new QueryWrapper<SchoolMode>()
//                    .eq("mode_id", deviceMode.getId()));
//            if (schoolModeList.size() > 0) {
//                deviceModeVO.setUsedFlag(1);
//            } else {
//                deviceModeVO.setUsedFlag(0);
//            }
//            modeVOList.add(deviceModeVO);
//        });
//        pageVo.setRecords(modeVOList);
//
//        return pageVo;
//    }
//
//    @Override
//    public List<DeviceMode> deviceModeList() {
//        return this.list(null);
//    }
//
//    @Override
//    public DeviceModeVO deviceModeInfo(String modeId) {
//        DeviceModeVO deviceModeVO = new DeviceModeVO();
//        DeviceMode deviceMode = this.getById(modeId);
//        BeanUtils.copyProperties(deviceMode, deviceModeVO);
//        List<SchoolMode> schoolModeList = schoolModeMapper.selectList(new QueryWrapper<SchoolMode>()
//                .eq("mode_id", modeId));
//        if (schoolModeList.size() > 0) {
//            deviceModeVO.setUsedFlag(1);
//        } else {
//            deviceModeVO.setUsedFlag(0);
//        }
//        return deviceModeVO;
//    }

    @Override
    public List<DeviceModeVO> getSchoolMode(String schoolId) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DeviceModeMapper.class);
            List<DeviceMode> modes = mapper.selectSchoolMode(schoolId);
            if (modes!=null && !modes.isEmpty()) {
                List<DeviceModeVO> modeVOS = new ArrayList<>();
                for (DeviceMode mode : modes) {
                    DeviceModeVO modeVO = new DeviceModeVO();
                    try {
                        BeanUtils.copyProperties(mode, modeVO);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                    modeVOS.add(modeVO);
                }
                return modeVOS;
            }
            return null;
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public DeviceMode getDefaultMode() {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DeviceModeMapper.class);
            return mapper.getDefaultMode();
        } finally {
            endOfSession(session);
        }
    }

    @Override
    public DeviceMode getById(String id) {
        try {
            session = DynamicDataSourceUtil.mysqlSessionFactory.openSession();
            mapper = session.getMapper(DeviceModeMapper.class);
            return mapper.selectById(id);
        } finally {
            endOfSession(session);
        }
    }

    public void endOfSession(SqlSession session) {
        //关闭sqlSession
        if (session != null) {
            log.info("session commit -> session close");
//            session.commit();
            session.close();
        }
    }
}
